/**
 * @param {string} s
 * @param {number} k
 * @return {string}
 */
var removeDuplicates = function (s, k) {
  if (s.length <= k) return s
  let arr = []
  let res = ''
  for(str of s){
    if(arr.length==0 || arr[arr.length-1][0]!=str){
      arr.push([str,1])
    }else{
      if(arr[arr.length-1][0]==str){
        if(arr[arr.length-1][1]+1==k){
          arr.pop()
        }else {
          arr[arr.length-1][1]++
        }
      }
    }
  }

  for(let i=0;i<arr.length;i++){
    res+=arr[i][0].repeat(arr[i][1])
  }

  return res
}

removeDuplicates('deeedbbcccbdaa',3)
dd